<?php

namespace app\company\controller;

use app\common\controller\CompanyController;
use app\common\enum\order\OrderStatus;
use app\common\enum\order\OrderType;
use app\common\enum\order\ReceiveType;
use app\common\model\DealerGoodsSpec;
use app\common\model\Driver;
use app\common\model\Salesman;
use app\common\model\SalesmanDealer;
use app\common\model\SalesmanStore;
use app\company\model\CompanyOrder;
use app\common\model\DealerGoods;
use app\common\model\Dealer;
use think\App;

class Report extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function sale(): \think\response\Json
    {
        $where = [
            ['pid','<>',0],
            ['company_id','=',$this->companyId],
            ['order_status','=',OrderStatus::FINISH],
            ['order_type', '=', OrderType::SALES],
        ];
        // 总销售金额
        $xs = (new CompanyOrder())->where($where)->sum('order_amount');
        // 收款金额
        $sk = $xs;
        // 欠款金额
        $qk = (new CompanyOrder())->where($where)->where('receive_type',ReceiveType::NOW)->sum('unsay_amount');
        // 预收款金额
        // 获取业务员
        $salesmanIds = (new Salesman())->where('company_id',$this->companyId)->column('id');
        // 查询充值
        $ysk = (new SalesmanStore())->where('id','in',$salesmanIds)->sum('balance');
        // 业务员数量
        $salesmanNum = (new Salesman())->where('')->count();
        // 司机数量
        $driverNum = (new Driver())->count();
        // 线路数量
        $lineNum = 0;
        // 商品数量
        $goodsNum = (new DealerGoods())->where('company_id',$this->companyId)->count();
        $jxs = (new Dealer())->field('id,name')->where('company_id',$this->companyId)->select()->each(function ($item) use ($where) {
            $item->xs = (new CompanyOrder())->where($where)->where('dealer_id',$item->id)->sum('order_amount');
            $item->sk = $item->xs;
            $item->qk = (new CompanyOrder())->where($where)->where('dealer_id',$item->id)->where('receive_type',ReceiveType::NOW)->sum('unsay_amount');
            // 获取业务员
            $salesmanIds = (new SalesmanDealer())->where('dealer_id',$this->uid)->column('salesman_id');
            // 查询充值
            $item->ysk = (new SalesmanStore())->where('salesman_id','in',$salesmanIds)->sum('balance');
        });
        $sale = (new Salesman())->field('id,rename')
            ->with([
                'dealer' => function ($query) {
                    $query->with([
                        'dealer' => function ($query) {
                            $query->field('id,name');
                        }
                    ]);
                }
            ])->where('company_id',$this->companyId)->select()->each(function ($item) use ($where) {
            $item->xs = (new CompanyOrder())->where($where)->where('salesman_id',$item->id)->sum('order_amount');
            $item->sk = $item->xs;
            $item->qk = (new CompanyOrder())->where($where)->where('salesman_id',$item->id)->where('receive_type',ReceiveType::NOW)->sum('unsay_amount');
            // 查询充值
            $item->ysk = (new SalesmanStore())->where('salesman_id',$item->id)->sum('balance');
        });
        $list = compact('xs','sk','qk','ysk','salesmanNum','driverNum','lineNum','goodsNum','jxs','sale');
        return $this->success($list);
    }
}